﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsDisputesTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Disputes Business Object
    /// </summary>

    public class Disputes : BOBase
    {

        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Disputes() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// DisputeID of the Disputes.
        /// </summary>
        public int DisputeID
        {
            get { return _disputeID; }
            set
            {
                if (_disputeID != value)
                {
                    if (OnPropertyChanging("DisputeID", _disputeID, value))
                    {
                        int oldValue = _disputeID;
                        _disputeID = value;

                        OnPropertyChanged("DisputeID", oldValue, value);
                    }
                }
            }
        }  private int _disputeID = -1;

        /// <summary>
        /// JobID.
        /// </summary>
        public string JobID
        {
            get { return _jobID; }
            set
            {
                if (_jobID != value)
                {
                    if (OnPropertyChanging("JobID", _jobID, value))
                    {
                        string oldValue = _jobID;
                        _jobID = value;
                        OnPropertyChanged("JobID", oldValue, value);
                    }
                }
            }
        }  private string _jobID = null;


        /// <summary>
        /// DisputeCategoryID.
        /// </summary>
        public int DisputeCategoryID
        {
            get { return _disputeCategoryID; }
            set
            {
                if (_disputeCategoryID != value)
                {
                    if (OnPropertyChanging("DisputeCategoryID", _disputeCategoryID, value))
                    {
                        int oldValue = _disputeCategoryID;
                        _disputeCategoryID = value;
                        OnPropertyChanged("DisputeCategoryID", oldValue, value);
                    }
                }
            }
        }  private int _disputeCategoryID = -1;


        /// <summary>
        /// DisputeSubCategory.
        /// </summary>
        public string DisputeSubCategory
        {
            get { return _disputeSubCategory; }
            set
            {
                if (_disputeSubCategory != value)
                {
                    if (OnPropertyChanging("DisputeSubCategory", _disputeSubCategory, value))
                    {
                        string oldValue = _disputeSubCategory;
                        _disputeSubCategory = value;
                        OnPropertyChanged("DisputeSubCategory", oldValue, value);
                    }
                }
            }
        }  private string _disputeSubCategory = null;



        /// <summary>
        /// RaisedBy.
        /// </summary>
        public int RaisedBy
        {
            get { return _raisedBy; }
            set
            {
                if (_raisedBy != value)
                {
                    if (OnPropertyChanging("RaisedBy", _raisedBy, value))
                    {
                        int oldValue = _raisedBy;
                        _raisedBy = value;
                        OnPropertyChanged("RaisedBy", oldValue, value);
                    }
                }
            }
        }  private int _raisedBy = -1;


        /// <summary>
        /// DisputeReason.
        /// </summary>
        public string DisputeReason
        {
            get { return _disputeReason; }
            set
            {
                if (_disputeReason != value)
                {
                    if (OnPropertyChanging("DisputeReason", _disputeReason, value))
                    {
                        string oldValue = _disputeReason;
                        _disputeReason = value;
                        OnPropertyChanged("DisputeReason", oldValue, value);
                    }
                }
            }
        }  private string _disputeReason = null;




        /// <summary>
        /// Date/time at which the Job was added to Disputes.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty Disputes object.
        /// </summary>
        public static Disputes CreateNew()
        {
            Disputes obj = new Disputes();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.AddedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static Disputes CreateNew(DisputesCollection collection)
        {
            Disputes obj = CreateNew();

            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllDisputes

        /// <summary>
        /// Returns a collection of all Disputes.
        /// </summary>
        public static DisputesCollection GetAllDisputes()
        {
            DisputesTableAdapter adapter = new DisputesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsDisputes.DisputesDataTable tbl = adapter.GetData(null);

            DisputesCollection list = new DisputesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsDisputes.DisputesRow row in tbl.Rows)
                {
                    Disputes disputes = new Disputes();
                    RelationalToObject(disputes, row);
                    list.Add(disputes);
                }
            }

            return (list);
        }

        #endregion

        #region GetAllDisputesByUserID

        /// <summary>
        /// Returns a collection of all Disputes.
        /// </summary>
        public static DisputesCollection GetAllDisputesByUserID(int userid)
        {
            DisputesTableAdapter adapter = new DisputesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsDisputes.DisputesDataTable tbl = adapter.GetDataByUserID(userid);

            DisputesCollection list = new DisputesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsDisputes.DisputesRow row in tbl.Rows)
                {
                    Disputes disputes = new Disputes();
                    RelationalToObject(disputes, row);
                    list.Add(disputes);
                }
            }

            return (list);
        }

        #endregion

        #region GetDisputesByJobID

        /// <summary>
        /// Returns a collection of all Disputes to check for duplicacy.
        /// </summary>
        public static Disputes GetDisputesByJobID(string jobid)
        {
            DisputesTableAdapter adapter = new DisputesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsDisputes.DisputesDataTable tbl = adapter.GetDataByJobID(jobid);

            if (tbl.Rows.Count == 1)
            {
                Disputes fl = new Disputes();
                RelationalToObject(fl, tbl.Rows[0]);
                return (fl);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'DisputeID' returned more than one record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified Disputes.
        /// </summary>
        public static void Update(Disputes obj)
        {
            bool updateDisputes = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateDisputes = (obj.ObjState != ObjectState.Unchanged);

                if (!updateDisputes)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsDisputes.DisputesDataTable tbl = null;
            if (updateDisputes)
            {
                DisputesTableAdapter adapter = new DisputesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsDisputes.DisputesDataTable();
                dsDisputes.DisputesRow row = tbl.NewDisputesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("UserID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("Disputes: DisputeID={0}, JobID={1}, {2}", DisputeID, JobID, RaisedBy));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more Disputes objects.
    /// </summary>

    public class DisputesCollection : List<Disputes>
    {
        #region Types

        public class UserComparer : IComparer<Disputes>
        {
            public int Compare(Disputes d1, Disputes d2)
            {
                return (String.Compare(d1.DisputeID.ToString(), d1.DisputeID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByDisputesUserID

        public Disputes FindByDisputesUserID(int userID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].RaisedBy == userID)
                    return (this[ndx]);

            return (null);
        }

        #endregion


        #endregion
    }
}
